import 'package:flutter/cupertino.dart';

class ColorGradient {
  factory ColorGradient(){
    final _instance = ColorGradient._();
    return _instance;
  }
  ColorGradient._();

  createGradient({startColor: 'rgb(0, 0, 0)',endColor: 'rgb(255, 255, 255)',step: 100}){
    List<dynamic> startRGB = _hexToRgb(startColor, str:false); //转换为rgb数组模式
    var startR = startRGB[0];
    var startG = startRGB[1];
    var startB = startRGB[2];

    List<dynamic> endRGB = _hexToRgb(endColor, str:false);
    var endR = endRGB[0];
    var endG = endRGB[1];
    var endB = endRGB[2];

    var sR = (endR - startR) / step; //总差值
    var sG = (endG - startG) / step;
    var sB = (endB - startB) / step;
    List<dynamic> colorArr = [];
    for (var i = 0; i < step; i++) {
      //计算每一步的hex值
      var hex = _rgbToHex('rgb(${(sR * i + startR).round()},${(sG * i + startG).round()},${(sB * i + startB).round()})');
      colorArr.add(hex);
    }
    return colorArr;
  }

  // 将hex表示方式转换为rgb表示方式(这里返回rgb数组模式)
  _hexToRgb(sColor, {str: true}){
    RegExp reg = new RegExp(r"^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$");
    sColor = sColor.toLowerCase();
    if (sColor != null && reg.hasMatch(sColor)) {
      if (sColor.length == 4) {
        String sColorNew = "#";
        for (var i = 1; i < 4; i += 1) {
          sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
        }
        sColor = sColorNew;
      }
      //处理六位的颜色值
      List<dynamic> sColorChange = [];
      for (var i = 1; i < 7; i += 2) {
        sColorChange.add(int.parse("0x" + sColor.substring(i, i + 2)));
      }
      if(!str) {
        return sColorChange;
      } else {
        return 'rgb(${sColorChange[0]},${sColorChange[1]},${sColorChange[2]})';
      }
    } else if (new RegExp(r'^(rgb|RGB)').hasMatch(sColor)) {
      List<dynamic> arr = sColor.replace(new RegExp(r'(?:\(|\)|rgb|RGB)*'), "").split(",");
      return arr.forEach((val)=>{ int.parse(val)});
    } else {
      return sColor;
    }
  }
}

 // 将rgb表示方式转换为hex表示方式
 _rgbToHex(rgb){
   var _this = rgb;
   RegExp reg = new RegExp(r'^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$');
   if (new RegExp(r'^(rgb|RGB)').hasMatch(_this)) {
     var aColor = _this.replaceAll(new RegExp(r'(?:\(|\)|rgb|RGB)*'), "").split(",");
     String strHex = "FF";
     for (var i = 0; i < aColor.length; i++) {
       String hex = int.parse(aColor[i]).toRadixString(16);
       hex = hex.length == 1 ? '0${hex}' : hex; // 保证每个rgb的值为2位
       if (hex == "0") {
         hex += hex;
       }
       strHex += hex;
     }
     int color = int.parse(strHex,radix: 16); //转化为int
     // print('color:${strHex},${color}');
     // if (strHex.length != 7) {
     //   strHex = _this;
     // }
     return color;
   } else if (reg.hasMatch(_this)) {
     String aNum = _this.replace(new RegExp('#'), "").split("");
     if (aNum.length == 6) {
      return _this;
     } else if (aNum.length == 3) {
       String numHex = "#";
       for (var i = 0; i < aNum.length; i += 1) {
         numHex += (aNum[i] + aNum[i]);
       }
       return numHex;
     }
   } else {
     return _this;
   }
 }

